package com.hibase.core.rule.web.rulemenu;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hibase.common.entity.ResponseModel;
import com.hibase.common.exception.HibaseException;
import com.hibase.common.util.EntityUtil;
import com.hibase.core.rule.constant.RuleStatus;
import com.hibase.core.rule.entity.ruledatasource.RuleDataSource;
import com.hibase.core.rule.entity.ruledebug.RuleDebugParamVO;
import com.hibase.core.rule.entity.rulemenu.RuleMenuContent;
import com.hibase.core.rule.entity.rulemenu.vo.RuleMenuContentInsertVO;
import com.hibase.core.rule.entity.rulemenu.vo.RuleMenuContentListVO;
import com.hibase.core.rule.entity.rulemenu.vo.RuleMenuContentUpdateVO;
import com.hibase.core.rule.entity.rulemenu.vo.RuleMenuContentViewVO;
import com.hibase.core.rule.service.ruledatasource.RuleDataSourceService;
import com.hibase.core.rule.service.ruleexecute.RuleExecuteService;
import com.hibase.core.rule.service.rulemenu.RuleMenuContentService;
import com.hibase.core.web.annotation.HibasePostRequestMapping;
import com.hibase.core.web.entity.OperationIdEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 规则菜单内容表 前端控制器
 * </p>
 *
 * @author hufeng
 * @since 2019-06-10
 */
@RestController
@RequestMapping("/rule-menu-content")
@Api(tags = "规则菜单内容表")
public class RuleMenuContentController{

    @Autowired
    private RuleMenuContentService ruleMenuContentService;

    @Autowired
    private DynamicDataSourceProperties properties;

    @Autowired
    private RuleExecuteService ruleExecuteService;


    @Autowired
    private RuleDataSourceService ruleDataSourceService;

    @ApiOperation(value = "新增规则菜单内容表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "insertRuleMenuContent")
    public ResponseModel<String> insertRuleMenuContent(@RequestBody RuleMenuContentInsertVO insertVO) {

        RuleMenuContent entity = EntityUtil.transform(insertVO, RuleMenuContent.class);

        ruleMenuContentService.saveRuleMenuContent(entity);

        return ResponseModel.successMsg(entity.getId());
    }

    @ApiOperation(value = "修改规则菜单内容表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "updateRuleMenuContent")
    public ResponseModel<String> updateRuleMenuContent(@RequestBody RuleMenuContentUpdateVO updateVO) {

        RuleMenuContent entity = EntityUtil.transform(updateVO, RuleMenuContent.class);

        ruleMenuContentService.saveRuleMenuContent(entity);

        return ResponseModel.successMsg(entity.getId());
    }

    @ApiOperation(value = "查询规则菜单内容表详情", httpMethod = "POST")
    @HibasePostRequestMapping(value = "viewRuleMenuContentDetails")
    public ResponseModel<RuleMenuContentViewVO> viewRuleMenuContentDetails(@RequestBody OperationIdEntity operationidEntity) {

        return ResponseModel.successMsg(EntityUtil.transformByJson(ruleMenuContentService.viewRuleMenuContent(operationidEntity.getId()), RuleMenuContentViewVO.class));
    }

    @ApiOperation(value = "删除规则菜单内容表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/deleteRuleMenuContent")
    public ResponseModel<String> deleteRuleMenuContent(@RequestBody OperationIdEntity delete) {

        ruleMenuContentService.deleteRuleMenuContent(delete.getId());

        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "发布规则菜单内容表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/releaseRuleMenuContent")
    public ResponseModel<String> releaseRuleMenuContent(@RequestBody OperationIdEntity delete) {
        
        ruleMenuContentService.operaRuleMenuContent(delete.getId(), RuleStatus.RULE_STATUS_RELEASE.getKey());

        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "下线规则菜单内容表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/cancelRuleMenuContent")
    public ResponseModel<String> cancelRuleMenuContent(@RequestBody OperationIdEntity delete) {

        ruleMenuContentService.operaRuleMenuContent(delete.getId(), RuleStatus.RULE_STATUS_CANCEL.getKey());

        return ResponseModel.successMsg();
    }

    @ApiOperation(value="获取数据源列表", httpMethod="POST")
    @HibasePostRequestMapping({"/selectDataSourceList"})
    public ResponseModel<String> selectDataSourceList()
    {
        Map<String, DataSourceProperty> sourceMap = this.properties.getDatasource();
        if (!CollUtil.isNotEmpty(sourceMap)) {
            throw new HibaseException("暂无可用数据源");
        }
        List<String> result = new ArrayList(sourceMap.keySet());

        Set<String> ruleDataSourceList = ruleDataSourceService.list().stream().map(RuleDataSource::getPollName).collect(Collectors.toSet());

        result.addAll(ruleDataSourceList);

        return ResponseModel.successMsg(result);
    }


    @ApiOperation(value = "数据源/规则调试",httpMethod = "POST")
    @HibasePostRequestMapping(value = "/debug")
    public ResponseModel<String> debug(@RequestBody RuleDebugParamVO paramVO){

        return ResponseModel.successMsg(ruleExecuteService.debug(paramVO));
    }

    @ApiOperation(value = "获取发布的规则列表")
    @HibasePostRequestMapping(value = "selectReleaseRuleList")
    public ResponseModel selectRule(){
        QueryWrapper<RuleMenuContent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status","RELEASE");
        List<RuleMenuContent> list = ruleMenuContentService.list(queryWrapper);
        return ResponseModel.successMsg(EntityUtil.transform(list, RuleMenuContentListVO.class));
    }

}
